//
//  YZCommunityLocationVC.m
//  VET
//
//  Created by YZ on 2018/7/11.
//  Copyright © 2018年 YZ. All rights reserved.
//

#import "YZCommunityLocationVC.h"
#import "YZCommunityLocationCell.h"
#import "YZCommunityLocationNoneCell.h"
#import "YZCommunityLocationViewModel.h"

@interface YZCommunityLocationVC ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UITableView *contentTb;

@property (nonatomic, strong) YZCommunityLocationViewModel *viewModel;

@property (nonatomic, assign) int currentSelectedIndex;

@end

@implementation YZCommunityLocationVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.currentSelectedIndex = -100;
    [self configUI];
    [self initUI];
    [self addObserver];
    [self.viewModel startRequestCommunityLocationWithCoor:((CLLocation *)[self valueForKey:@"currentLocation"]).coordinate];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.tabBarController.tabBar setHidden:YES];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [self.tabBarController.tabBar setHidden:NO];
}

- (void)addObserver
{
    [RACObserve(self.viewModel,loading) subscribeNext:^(id  _Nullable x) {
        if ([x boolValue]) {
            [SVProgressHUD show];
        }else
        {
            [self.contentTb reloadData];
            [SVProgressHUD dismiss];
        }
    }];
}

- (void)configUI
{
    self.view.backgroundColor = kColorWithHex(0xE9EAEB);
    self.title = @"所在位置";
}

- (void)initUI
{
    [self.view addSubview:self.contentTb];
}

#pragma mark -- UITableViewDelegate,UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.viewModel.dataSource.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.viewModel.dataSource[section].count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell;
    if(indexPath.row == 0)
    {
        static NSString *cellId = @"YZCommunityLocationNoneCell";
        cell = [tableView dequeueReusableCellWithIdentifier:cellId];
        if(!cell){
            cell = [[YZCommunityLocationNoneCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
        }
        YZCommunityLocationModel *model = [self.viewModel modelInSection:indexPath.section row:indexPath.row];
        if ([self.currentAddressName isEqualToString:@"所在位置"]) {
            model.selected = YES;
        }else
        {
            model.selected = [model.addressName isEqualToString:self.currentAddressName];
        }
        
        if (model.selected) {
            self.currentSelectedIndex = indexPath.row;
        }
        ((YZCommunityLocationNoneCell *)cell).model = model;
    }else
    {
        static NSString *cellId = @"YZCommunityLocationCell";
        cell = [tableView dequeueReusableCellWithIdentifier:cellId];
        if(!cell){
            cell = [[YZCommunityLocationCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
        }
        YZCommunityLocationModel *model = [self.viewModel modelInSection:indexPath.section row:indexPath.row];
        model.selected = [model.addressName isEqualToString:self.currentAddressName];
        ((YZCommunityLocationCell *)cell).model = model;
        if (model.selected) {
            self.currentSelectedIndex = indexPath.row;
        }
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.currentSelectedIndex != indexPath.row) {
        YZCommunityLocationNoneModel *preSelectedModel = [self.viewModel modelInSection:indexPath.section row:self.currentSelectedIndex];
        preSelectedModel.selected = NO;
        
        YZCommunityLocationNoneModel *curSelectedModel = [self.viewModel modelInSection:indexPath.section row:indexPath.row];
        curSelectedModel.selected = YES;
        
        [self.contentTb reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:self.currentSelectedIndex inSection:indexPath.section],[NSIndexPath indexPathForRow:indexPath.row inSection:indexPath.section]] withRowAnimation:UITableViewRowAnimationNone];
        self.currentSelectedIndex = indexPath.row;
    }
    
    if (_delegate && [_delegate respondsToSelector:@selector(didSelectedLocationItem:)]) {
        [_delegate didSelectedLocationItem:[self.viewModel  modelInSection:indexPath.section row:indexPath.row]];
    }
    [self.navigationController popViewControllerAnimated:YES];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.row == 0){
        return 44;
    }else
    {
        return UITableViewAutomaticDimension;
    }
}

#pragma mark -- setters && getters

- (UITableView *)contentTb
{
    if(!_contentTb){
        _contentTb = [[UITableView alloc] initWithFrame:CGRectMake(0, kCurrentDeviceNum(20), kScreenWidth, kScreenHeight - (kStatusBarHeight + kTopViewHeight + YZ_BOTTOM_MARGIN + kCurrentDeviceNum(20))) style:UITableViewStylePlain];
        _contentTb.showsVerticalScrollIndicator = NO;
        _contentTb.tableFooterView = [UIView new];
        _contentTb.delegate = self;
        _contentTb.dataSource = self;
        _contentTb.estimatedRowHeight = kCurrentDeviceNum(44);
    }
    return _contentTb;
}

- (YZCommunityLocationViewModel *)viewModel
{
    if(!_viewModel){
        _viewModel = [YZCommunityLocationViewModel new];
    }
    return _viewModel;
}
@end
